package com.logic.modular.game.battle.process.battleStage.actionPhase.actionPhase2.item;

import com.logic.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.comment.pojo.domain.logic.core.CoreSkillEffect;
import com.logic.modular.gameCore.match.battle.service.impl.CoreSkillEffectServiceImpl;
import com.comment.pojo.domain.logic.core.CoreSkillStar;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 4-2
 * 读取技能效果->获取技能效果目标
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ReleaseSkillService2 {
    private final Cache<String, List<PlayerHeroAttribute>> preTargetCache = Caffeine.newBuilder().build();
    private final CoreSkillEffectServiceImpl coreSkillEffectServiceImpl;
    @Resource
    private PlayerHeroAttributeServiceImpl playerHeroAttributeService;

    /**
     * 1 读取技能效果表
     */
    public List<CoreSkillEffect> list(
            CoreSkillStar coreSkillStar
    ) {
        Long skillId = coreSkillStar.getSkillId();
        return coreSkillEffectServiceImpl.listCache().stream()
                .filter(item -> Objects.equals(item.getCoreSkillId(), skillId))
                .collect(Collectors.toList());
    }

    /**
     * 2
     * 获取技能效果 -释放目标
     */
    public List<PlayerHeroAttribute> getTargetHeroes(
            Long battleId,
            @NotNull(message = "目标类型Id不能为空") Integer targetType,
            @NotNull(message = "敌军武将不能为空") List<PlayerHeroAttribute> defCamp,
            @NotNull(message = "攻击者武将不能为空") PlayerHeroAttribute actWJ,
            @NotNull(message = "攻击方阵营不能为空") List<PlayerHeroAttribute> actCamp
    ) {
        List<PlayerHeroAttribute> preTargetList = preTargetCache.getIfPresent(String.format("preTargetCache:%d", battleId));
        // @todo 26	暴走	敌我不分，无差别攻击，会在除自己之外的友方2个、敌方3个目标中来寻找目标进行攻击（或者治疗）（控制状态）
        Random random = new Random();
        List<PlayerHeroAttribute> targetList = new ArrayList<>();
        switch (targetType) {
            case 1: // 1 敌军单体 敌军随机1个单体
                if (defCamp.size() == 1) {
                    targetList.addAll(defCamp);
                } else {
                    targetList.add(defCamp.get(random.nextInt(defCamp.size())));
                }
                break;
            case 2: // 2 敌军群体 敌军随机2个目标
                if (defCamp.size() == 2) {
                    targetList.addAll(defCamp);
                }
                if (defCamp.size()==1){
                    targetList.add(defCamp.get(0));
                }
                if (defCamp.size()==3){
                    int i1 = random.nextInt(defCamp.size());
                    targetList.add(defCamp.get(i1));
                    int i2 = i1; // 初始化i2为i1，防止下标相同
                    while (i2 == i1) { // 确保两个索引不同
                        i2 = random.nextInt(defCamp.size());
                    }
                    targetList.add(defCamp.get(i2));
                }
                break;
            case 3: // 3 敌军全体 敌军全体3个目标
                if (defCamp.size() <= 3) {
                    targetList.addAll(defCamp);
                }
                break;
            case 4: // 4 自身 发动战法的武将自身
                targetList.add(actWJ);
                break;
            case 5: // 5 敌军和友军 敌军和友军随机1个目标
                targetList.add(actCamp.get(random.nextInt(actCamp.size())));
                targetList.add(defCamp.get(random.nextInt(defCamp.size())));
                break;
            case 6: // 6 友军全体 除自己之外的另外2个友军中
                actCamp.remove(actWJ);
                targetList.addAll(actCamp);
                actCamp.add(actWJ);
                break;
            case 7: // 7 我军单体 我军3个武将中随机1个单体
                targetList.add(actCamp.get(random.nextInt(actCamp.size())));
                break;
            case 8: // 8 我军群体 我军3个武将中随机2个武将
                if (actCamp.size() < 3) {
                    targetList.addAll(actCamp);
                } else {
                    PlayerHeroAttribute accountHero = actCamp.get(random.nextInt(actCamp.size()));
                    actCamp.remove(accountHero);
                    targetList.addAll(actCamp);
                    actCamp.add(accountHero);
                }
                break;
            case 9: // 9 我军全体 我军全体3个武将
                targetList.addAll(actCamp);
                break;
            case 10: // 10 自身和友军单体 武将自身和随机一个友军单位
                targetList.add(actWJ);
                if (actCamp.size() > 2) {
                    actCamp.remove(actWJ);
                    targetList.add(actCamp.get(random.nextInt(actCamp.size())));
                    actCamp.add(actWJ);
                }
                break;
            case 11: // 11 攻击目标 之前攻击过的目标，主要是在追击战法中出现
                if (preTargetList != null && !preTargetList.isEmpty()) {
                    targetList.add(preTargetList.get(random.nextInt(preTargetList.size())));
                }
                break;
            case 12: // 12 敌我全军 敌军和我军6个目标
                targetList.addAll(actCamp);
                targetList.addAll(defCamp);
                break;
        }
        return targetList;
    }


    /**
     * 2
     * 获取技能效果 -释放目标
     */
    public List<PlayerHeroAttribute> getTargetHeroes(
            String battleUUID,
            Long playerHeroId,
            Integer trigger6
    ) {

        List<PlayerHeroAttribute> actionList = playerHeroAttributeService
                .listCache(battleUUID).stream().filter(item -> item.getCurrentForce() > 0).collect(Collectors.toList());

        PlayerHeroAttribute actWJ = playerHeroAttributeService.get(battleUUID, playerHeroId);
        Integer rankCamp = actWJ.getRankCamp();
        List<PlayerHeroAttribute> actCamp = actionList.stream()
                .filter(item -> item.getRankCamp().equals(rankCamp)).collect(Collectors.toList());
        List<PlayerHeroAttribute> defCamp = actionList.stream()
                .filter(item -> !item.getRankCamp().equals(rankCamp)).collect(Collectors.toList());


        Random random = new Random();
        List<PlayerHeroAttribute> targetList = new ArrayList<>();
        switch (trigger6) {
            case 1: // 1 敌军单体 敌军随机1个单体
                if (defCamp.size() == 1) {
                    targetList.addAll(defCamp);
                } else {
                    targetList.add(defCamp.get(random.nextInt(defCamp.size())));
                }
                break;
            case 2: // 2 敌军群体 敌军随机2个目标
                if (defCamp.size() == 2) {
                    targetList.addAll(defCamp);
                }
                if (defCamp.size()==1){
                    targetList.add(defCamp.get(0));
                }
                if (defCamp.size()==3){
                    int i1 = random.nextInt(defCamp.size());
                    targetList.add(defCamp.get(i1));
                    int i2 = i1; // 初始化i2为i1，防止下标相同
                    while (i2 == i1) { // 确保两个索引不同
                        i2 = random.nextInt(defCamp.size());
                    }
                    targetList.add(defCamp.get(i2));
                }
                break;
            case 3: // 3 敌军全体 敌军全体3个目标
                if (defCamp.size() <= 3) {
                    targetList.addAll(defCamp);
                }
                break;
            case 4: // 4 自身 发动战法的武将自身
                targetList.add(actWJ);
                break;
            case 5: // 5 敌军和友军 敌军和友军随机1个目标
                targetList.add(actCamp.get(random.nextInt(actCamp.size())));
                targetList.add(defCamp.get(random.nextInt(defCamp.size())));
                break;
            case 6: // 6 友军全体 除自己之外的另外2个友军中
                actCamp.remove(actWJ);
                targetList.addAll(actCamp);
                actCamp.add(actWJ);
                break;
            case 7: // 7 我军单体 我军3个武将中随机1个单体
                targetList.add(actCamp.get(random.nextInt(actCamp.size())));
                break;
            case 8: // 8 我军群体 我军3个武将中随机2个武将
                if (actCamp.size() < 3) {
                    targetList.addAll(actCamp);
                } else {
                    PlayerHeroAttribute accountHero = actCamp.get(random.nextInt(actCamp.size()));
                    actCamp.remove(accountHero);
                    targetList.addAll(actCamp);
                    actCamp.add(accountHero);
                }
                break;
            case 9: // 9 我军全体 我军全体3个武将
                targetList.addAll(actCamp);
                break;
            case 10: // 10 自身和友军单体 武将自身和随机一个友军单位
                targetList.add(actWJ);
                if (actCamp.size() > 2) {
                    actCamp.remove(actWJ);
                    targetList.add(actCamp.get(random.nextInt(actCamp.size())));
                    actCamp.add(actWJ);
                }
                break;
            case 11: // 11 攻击目标 之前攻击过的目标，主要是在追击战法中出现
                log.error("触发目标");
                break;
            case 12: // 12 敌我全军 敌军和我军6个目标
                targetList.addAll(actionList);
                break;
        }
        return targetList;
    }
}
